<?php
/**
 * 
 * @authors Lin (callbackbing@163.com)
 * @date    2014-01-16 15:17:30
 * @version $Id$
 */

class SuffixAction extends CommonAction {
    
    /*后缀词列表*/
    public function index(){
        $agent_id = $_SESSION['agent']['id']; 

        import("ORG.Util.Page");
        $pagenumber = 10;

        if($_GET['type'] == 'mine'){
            $proxyid = $agent_id;
        }else{
            $proxyid = 0;
        }

        $suffix_arr = M()->table("yst_houzhuitype t,yst_houzhui h")
        ->field("t.name as type_name,h.name,h.id")
        ->where("t.id = h.type and t.proxyid = $proxyid")
        ->page($_GET['p'])->limit($pagenumber)
        ->order('h.id desc')
        ->select();

        $totalnum = count(M('houzhui')->where("proxyid = $proxyid")->select());

        $page = new Page($totalnum, $pagenumber);
        $page->setConfig("prev", "上一页");
        $page->setConfig("next", "下一页");
        $page->setConfig("first", "首页");
        $page->setConfig("last", "末页");
        $page->setConfig("theme", "<span class=pageinfo> <u></u>当前%nowPage%/%totalPage%页</span>  %first% %upPage%   %linkPage%  %downPage% %end%");
        $pageshow = $page->show();
        $this->assign("pageshow", $pageshow);

        $this->assign('suffix_arr',$suffix_arr);
        $this->display();
    }

    /*后缀词类型*/
    public function suffix_type(){
        $agent_id = $_SESSION['agent']['id'];

        if(IS_POST){
            $data['proxyid'] = $agent_id;
            $str = I('post.type_name');

            $arr = explode("\r\n", $str);

            foreach ($arr as $v) {

                $result = M('houzhuitype')->where("name='".$v."' and proxyid=$agent_id")->select();
                
                if(!$result){

                    if($v){
                        $data['name'] = $v;
                        $result = M('houzhuitype')->add($data);                    
                    }else{
                        $ajax['Msg'] = '请正确填写表单!';
                        echo json_encode($ajax);
                        die();
                    }

                }else{
                        $ajax['Msg'] = '已经存在该后缀词类型!';
                        echo json_encode($ajax);
                        die();
                }

            }

            if($result){
                $ajax['status'] = 'true';
                $ajax['Msg'] = '保存成功!';
                $ajax['url'] = '/Suffix/suffix_type_control';
                echo json_encode($ajax);
            }else{
                $ajax['Msg'] = '保存失败!';
                echo json_encode($ajax);
            }
            die();
        }
        $this->display();
    }

    /*后缀词类型管理*/
    public function suffix_type_control(){

        $agent_id = $_SESSION['agent']['id'];
        import("ORG.Util.Page");
        $pagenumber = 8;

        $stye_arr = M('houzhuitype')->where("proxyid = $agent_id")->field('id,name')
        ->page($_GET['p'])->limit($pagenumber)
        ->order('id desc')->select();

        $totalnum = count(M('houzhuitype')->where("proxyid = $agent_id")->select());

        $page = new Page($totalnum, $pagenumber);
        $page->setConfig("prev", "上一页");
        $page->setConfig("next", "下一页");
        $page->setConfig("first", "首页");
        $page->setConfig("last", "末页");
        $page->setConfig("theme", "<span class=pageinfo> <u></u>当前 %nowPage%/%totalPage%页</span>  %first% %upPage%   %linkPage%  %downPage% %end%");
        $pageshow = $page->show();
        $this->assign("pageshow", $pageshow);
        $this->assign('stye_arr',$stye_arr);
        $this->display();
    }

    /*后缀词类型删除处理*/
    public function suffix_type_del(){

        $id = I('post.userId');
        $this->del('houzhuitype', $id);

        //同时删除其类型下面的后缀词
        M('houzhui')->where("type=$id")->delete();

    }

    /*后缀词删除处理*/
    public function suffix_del(){

        $id = I('post.userId');
        $this->del('houzhui', $id);      
    }

    /*添加后缀词*/
    public function suffix_add(){
        $agent_id = $_SESSION['agent']['id'];
        $type_arr = M('houzhuitype')->where("proxyid=$agent_id")->field('id,name')->select();
        $this->assign('type_arr',$type_arr);

        if(IS_POST){

            $data['type'] = I('post.domain');
            $data['proxyid'] = $agent_id;
            $arr = I('post.keywords');

            foreach ($arr as $v) {
                $data['name'] = $v;

                $rs = M('houzhui')->where($data)->find();

                //如果没有可以添加进去
                if(!$rs){
                    $result = M('houzhui')->add($data);
                }

            }

            if($result){
                $ajax['status'] = 'true';
                $ajax['Msg'] = '添加成功!';
                $ajax['url'] = '/Suffix/index/type/mine';
                echo json_encode($ajax);
            }else{
                $ajax['Msg'] = '存在该类的后缀词!';
                echo json_encode($ajax);                
            }
            die();
        }

        $this->display();
    }

}